var url = require('url');
var template = require('templateConfig');
var main = require('main');
var form = require('form');
var module= require('module');

//模板配置
var tplArr = [];
var Tpl =require('raw!../template/evaluate.txt'); 
tplArr['evaluate'] = template.compile(Tpl.replace(/^\s*|\s*$/g, "")); 
 
var productNo = main.GetQueryString('product_no');//
var orderId = main.GetQueryString('order_id');//获取任务id

function Index(){
    _this = this;
    this.main = function(){
        main.event(this,'tap','i-click'); 
        this.init();
    }    
    this.ddd=function(){
        var url_current = window.location.href;
        main.post(url.jsok,{"url":url_current},function(rs){
            wx.config(rs);
            // wx.debug=true;
        })
    }   
    this.getContent= function() {
    	var starSingle= {
    		dataList: [{
		    key: "评价",
		    value: "4",
		    bigStar: true
		}]}; 
    	var starInfo = {
    		dataList: [{
		    key: "商品包装",
		    value: "0",
		    click: "starClick"
		},{
		    key: "送货速度",
		    value: "0",
		    click: "starClick"
		},{
		    key: "配送员服务态度",
		    value: "0",
		    click: "starClick"
		}]
    	};
    	var ordersGoodsStar= {
    		title: "大家乐文胸一片式无痕内衣厚B杯深V聚拢商场同款内衣1",
    		star: module.getModule(starSingle,"star")
    	}
    	var evaluate= {};
    	var data= {
    		tType: "content",
    		ordersGoodsStar: module.getModule(ordersGoodsStar, "ordersGoodsStar"),
    		evaluate: module.getModule(evaluate, "evaluate"),
    		starList: module.getModule(starInfo, "star"),
    		btnName: "确认提交",
    		link: "javascript:;",  
    		click: "upload_commit",
    	}
    	$(".content").html(tplArr['evaluate'](data));
    }
    //上传照片
    this.uploadPic=function(ths,m,e){

        wx.ready(function (){
            //本地相册调用
            wx.chooseImage({
                
                count: 1, // 默认9
                // sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                // sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    var localIds = res.localIds[0]; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                    _this.urlImage = localIds;
                    $(".upload_s span").css("background-image", "url("+localIds+")"); 
                    $("[name=localId]").val(localIds);
                    // $("[name=openid]").val(openId);
                    // alert('123'+localIds);
                    wx.uploadImage({
                        localId: localIds, // 需要上传的图片的本地ID，由chooseImage接口获得
                        isShowProgressTips: 1, // 默认为1，显示进度提示
                        success: function (res) {
                            var serverId = res.serverId; // 返回图片的服务器端ID
                            $("[name=serverId]").val(serverId); 
                            // alert("345"+serverId);
                        }
                    });
                }
            }); 
        });
    }
    //确认提交
    this.upload_commit=function(ths){   
        var packing=$(".star_0 >i.on").size();
        var speed=$(".star_1 >i.on").size();
        var service=$(".star_2 >i.on").size();
        var anNum;
        if($("input[name='anony']:checked").length==1){
           anNum=1;
        }else{
            anNum=2;
        }
        console.log(productNo+'<>'+orderId);
        $("[name=anony]").val(anNum);
        $("[name=speed_grade]").val(speed);
        $("[name=packing_grade]").val(packing);
        $("[name=service_grade]").val(service);
        $("[name=product_no]").val(productNo);
        $("[name=order_id]").val(orderId);

        if($(".star_0 >i.on").size()==0||$(".star_1 >i.on").size()==0||$(".star_2 >i.on").size()==0){
          main.alert({
                "title": "提示",
                "content": "请对所有评价项进行评价",
          })
          return false;
        }
    
        if(ths.attr('isSubmit') == 'false'){return false;}
        form.ajaxSubmit('.upload_style',_this,ths); 
    }
    //callback
    this.formCallback = function(ret){
        if (ret.code == 200) {
            window.location.href=url.Index;
        }
    }
    this.getFoot =function() {
        main.post(url.getCount,{},function(ret){
            var data = {
                active: "1",
                dataList: [{
                    link: url.Index,
                    icon: "ui-icon-home",
                    name: "首页"
                },{
                    link: url.shopCar,
                    icon: "ui-icon-cart",
                    name: "购物车",
                    cornernum: ret.data
                },{
                    link: url.Center,
                    icon: "ui-icon-personal",
                    name: "个人中心"
                }]
            }
            $(".wrap").append(module.getModule(data, "footer"));
        })
    }

    this.init= function(){   
        this.ddd(); 
        this.getContent();   
        this.getFooter();
        $(document).find(".upload_style").attr("action",url.add_goods_comment);            
    }
    return this.main();
};

var index = new Index();